﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Sale_Operation;
using Sale_Common;
using SaleSystem;
using Sale_Model;
namespace SaleSystem.BaseData
{
    public partial class frm_CustomerList : PageBase
    {
        #region PageEvent
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                bnNew.OnClientClick = win_Content.GetShowReference("frm_CustomerEdit.aspx?Id=0", "新增客户");
                BindDropDownList();
                GetAndBind();
            }
            else if (Request[LinkService.RefreshTag] == LinkService.RefreshString)
            {
                GetAndBind();
            }
        }
        protected void bnSearch_Click(object sender, EventArgs e)
        {
            GetAndBind();
        }

        protected void gv_List_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "delete")
            {
                Sale_Model.Customer SM = new Sale_Model.Customer();

                SM.Cust_Id = ValueHandler.GetIntNumberValue(gv_List.DataKeys[e.RowIndex][0]);
                SM.Cust_Name = ValueHandler.GetStringValue(gv_List.DataKeys[e.RowIndex][1]);
                bool blIsUsing = LinkService.CheckIsUsing("Assciation_Info", Id.ToString());
                if (!blIsUsing)
                {

                    bool blDelete = Manager.Delete(SM);
                    if (blDelete)
                    {
                        GetAndBind();
                        ExtAspNet.Alert.Show("删除完成！", ExtAspNet.MessageBoxIcon.Information);
                    }
                    else
                        ExtAspNet.Alert.Show("删除失败！", ExtAspNet.MessageBoxIcon.Error);
                }
                else
                    ExtAspNet.Alert.Show("该数据项正在使用，不能删除！", ExtAspNet.MessageBoxIcon.Warning);
            }
            else if (e.CommandName.ToLower() == "edit")
            {
                win_Content.Hidden = false;
                win_Content.IFrameUrl = "frm_CustomerEdit.aspx?Id=" + ValueHandler.GetIntNumberValue(gv_List.DataKeys[e.RowIndex][0]);
                win_Content.Title = "编辑-" + ValueHandler.GetStringValue(gv_List.DataKeys[e.RowIndex][1]);

            }
        }
        protected void gv_List_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            gv_List.PageIndex = e.NewPageIndex;
            GetAndBind();
        }
        protected void gv_List_RowDataBound(object sender, ExtAspNet.GridRowEventArgs e)
        {

            DataRowView row = e.DataItem as DataRowView;
            if (row != null)
            {
                if (ValueHandler.GetIntNumberValue(row["Cust_Sex"]) == 1)
                    e.Values[3] = "男";
                else
                    e.Values[3] = "女";
            }

        }
        #endregion

        #region Method
  
        private void GetAndBind()
        {
            FormatGrid(gv_List);
            InitCondition();
            dtList = Manager.GetList(strCondition, gv_List.PageIndex + 1, ref iRecordCount);
            gv_List.DataSource = dtList;
            gv_List.DataBind();
            gv_List.RecordCount = iRecordCount;
        }
        /// <summary>
        /// 获取查询条件
        /// </summary>
        public override void InitCondition()
        {
            if (tb_Cust_Code.Text.Trim() != "")
            {
                strCondition += " AssociationId = " + ValueHandler.GetIntNumberValue(tb_Cust_Code.Text) + "AND ";
            }
            if (tb_Cust_Name.Text.Trim() != "")
            {
                strCondition += " AssociationName LIKE '%" + ValueHandler.GetStringValue(tb_Cust_Name.Text, ValueHandler.CharHandlerType.IsRepChar) + "%' AND ";
            }
           // if (ddl_Cust_BData_Id.SelectedValue != "-1")
            //{
            //    strCondition += " Cust_BData_Id ='" + ddl_Cust_BData_Id.SelectedValue + "' AND ";
           // }
            //if (tb_Cust_IdentityCode.Text.Trim() != "")
            //{
            //    strCondition += " Cust_IdentityCode LIKE '%" + ValueHandler.GetStringValue(tb_Cust_IdentityCode.Text, ValueHandler.CharHandlerType.IsRepChar) + "%' AND ";
           // }
            //if (tb_Cust_Tel1.Text.Trim() != "")
            //{
            //    strCondition += " Cust_Tel1 LIKE '%" + ValueHandler.GetStringValue(tb_Cust_Tel1.Text, ValueHandler.CharHandlerType.IsRepChar) + "%' AND ";
           // }
            //if (tb_Cust_Tel2.Text.Trim() != "")
            //{
            //    strCondition += " Cust_Tel1 LIKE '%" + ValueHandler.GetStringValue(tb_Cust_Tel2.Text, ValueHandler.CharHandlerType.IsRepChar) + "%' AND ";
           // }
            //if (tb_Email.Text.Trim() != "")
            //{
            //    strCondition += " Email LIKE '%" + ValueHandler.GetStringValue(tb_Email.Text, ValueHandler.CharHandlerType.IsRepChar) + "%' AND ";
            //}
           // if (tb_QQ.Text.Trim() != "")
            //{
             //   strCondition += " QQ LIKE '%" + ValueHandler.GetStringValue(tb_QQ.Text, ValueHandler.CharHandlerType.IsRepChar) + "%' AND ";
           // }
            if (!string.IsNullOrEmpty(strCondition))
                strCondition = strCondition.Substring(0, strCondition.Length - 4);

        }
        private void BindDropDownList()
        {
            BaseDataOp Manager = new BaseDataOp();
            //客户类型
            //DataTable dtList1 = Manager.GetList("BData_Type=2 ");
            //ddl_Cust_BData_Id.DataSource = dtList1;
            //ddl_Cust_BData_Id.DataBind();
            //ddl_Cust_BData_Id.Items.Insert(0, new ExtAspNet.ListItem("-未选择-", "-1"));
            
        }
        #endregion

        #region Property
        CustomerOp Manager = new CustomerOp();

        #endregion

      
    }
}
